<template>
  <base-breadcrumb :title="titleName">
    <template v-slot:content>
      <a-card :bordered="false">
        <div class="table-page-search-wrapper">
          <title-name title="项目信息" />
          <a-form layout="horizontal">
            <a-row :gutter="24">
              <a-col
                :md="16"
                :sm="16"
              >
                <a-form-model-item
                  label="所属单位"
                  :label-col="{ lg: { span: 7 }, sm: { span: 7 } }"
                  :wrapper-col="{ lg: { span: 17 }, sm: { span: 17 } }"
                >
                  <!--                              v-model="form.branchCompanyName"-->
                  <BaseInput
                    :value='unit'
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <!--              <a-col-->
              <!--                :md="8"-->
              <!--                :sm="8"-->
              <!--              >-->
              <!--                <a-form-item-->
              <!--                  label="分公司名称"-->
              <!--                  :label-col="{lg: {span: 7}, sm: {span:7}}"-->
              <!--                  :wrapper-col="{lg: {span: 17}, sm: {span: 17} }"-->
              <!--                >-->
              <!--                  <BaseInput-->
              <!--                    v-model="branchName"-->
              <!--                    disabled-->
              <!--                  />-->
              <!--                </a-form-item>-->
              <!--              </a-col>-->
              <!--              <a-col-->
              <!--                :md="8"-->
              <!--                :sm="8"-->
              <!--              >-->
              <!--                <a-form-item-->
              <!--                  label="项目名称"-->
              <!--                  :label-col="{lg: {span: 7}, sm: {span:7}}"-->
              <!--                  :wrapper-col="{lg: {span: 17}, sm: {span: 17} }"-->
              <!--                >-->
              <!--                  <BaseInput-->
              <!--                    v-model="projectName"-->
              <!--                    disabled-->
              <!--                  />-->
              <!--                </a-form-item>-->
              <!--              </a-col>-->
            </a-row>
            <template>
              <a-row :gutter="24">
                <a-col
                  :md="8"
                  :sm="8"
                >
                  <a-form-item
                    label="当前人员数量"
                    :label-col="{lg: {span: 7}, sm: {span:7}}"
                    :wrapper-col="{lg: {span: 17}, sm: {span: 17} }"
                  >
                    <BaseInput
                      v-model="currentNum"
                      disabled
                      placeholder=""
                    />
                  </a-form-item>
                </a-col>
                <a-col
                  :md="8"
                  :sm="8"
                >
                  <a-form-item
                    label="领导班子人员数量"
                    :label-col="{lg: {span: 7}, sm: {span:7}}"
                    :wrapper-col="{lg: {span: 17}, sm: {span: 17} }"
                  >
                    <BaseInput
                      v-model="managerNum"
                      disabled
                      placeholder=""
                    />
                  </a-form-item>
                </a-col>
                <a-col
                  :md="8"
                  :sm="8"
                >
                  <a-form-item
                    label="其他岗位人员数量"
                    :label-col="{lg: {span: 7}, sm: {span:7}}"
                    :wrapper-col="{lg: {span: 17}, sm: {span: 17} }"
                  >
                    <BaseInput
                      v-model="otherNum"
                      disabled
                      placeholder=""
                    />
                  </a-form-item>
                </a-col>
              </a-row>
            </template>
          </a-form>
        </div>
      </a-card>
    </template>

    <a-card :bordered="false">
      <title-name title="项目领导班子人员" />
      <a-table
        :columns="columns"
        :data-source="projectPost"
        bordered
        :pagination="false"
        row-key="id"
      >
        <span
          slot="serial"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <span
          slot="jobName"
          slot-scope="text, record, index"
        >
          <template>
            <span
              v-if=" record.requiredCode == 1 "
              class="requireRed"
            >*</span> {{ record.jobName }} ({{ record.canBothName }},{{ record.numberLimitName }})
          </template>
        </span>
        <span
          slot="employeeName"
          slot-scope="text, record, index"
        >
          <!--<template>-->
          <!--{{record.staffList.map(s=>s.staffName).join(',')}}-->
          <!--</template>-->

          <template>
            <span
              v-for="(staff,index) in record.staffList"
              :key="index"
            >
              <span v-if="index !=0 ">,</span>
              <span
                v-if="staff.statusCode == 0 "
                style="color:rgba(0, 0, 0, 0.65)"
              >{{ staff.staffName }}</span>
              <span
                v-else-if="staff.statusCode == 2 "
                style="color:red;"
              >{{ staff.staffName }}</span>
              <span
                v-else-if="staff.statusCode != 0 && staff.statusCode != 2"
                style="color:red;"
                @click="handleApproveDetail(staff)"
              >{{ staff.staffName }}(审批信息)</span>
            </span>
          </template>

        </span>
        <span
          slot="employeeNum"
          slot-scope="text, record, index"
        >
          <template>
            <span
              v-for="(staff,index) in record.staffList"
              :key="index"
            >
              <span v-if="index !=0 ">,</span>
              <span
                v-if="staff.statusCode == 0 "
                style="color:rgba(0, 0, 0, 0.65)"
              >{{ staff.staffCode }} </span>
              <span
                v-else-if="staff.statusCode != 0 "
                style="color:red;"
              >{{ staff.staffCode }}</span>
            </span>
          </template>
        </span>
        <span
          slot="status"
          slot-scope="text, record, index"
        >
          <template>
            {{ record.staffList.map(s=>s.statusName).join(',') }}
          </template>
        </span>
        <span
          slot="operation"
          slot-scope="text, record, index"
        >
          <template>
            <a @click="handleSub(record)">查看</a>
            <span v-if=" type == 1 ">
              <a-divider type="vertical" /><a @click="handleEdit(record)">编辑</a>
            </span>
            <!--<span><a-divider type="vertical"/><a @click="handleApprove(record)">审批</a></span>-->
          </template>
        </span>
      </a-table>
      <title-name
        title="项目其他岗位人员"
        style="margin-top: 20px;"
      />
      <a-table
        :columns="columns"
        :data-source="otherPost"
        bordered
        :pagination="false"
        row-key="id"
      >
        <span
          slot="serial"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <span
          slot="jobName"
          slot-scope="text, record, index"
        >
          <!--<template>-->
          <!--{{record.staffList.map(s=>s.staffName).join(',')}}-->
          <!--</template>-->
          <template>
            <span
              v-if=" record.requiredCode == 1 "
              class="requireRed"
            >*</span> {{ record.jobName }} ({{ record.canBothName }} ,{{ record.numberLimitName }})
          </template>
        </span>
        <span
          slot="employeeName"
          slot-scope="text, record, index"
        >
          <!--<template>-->
          <!--{{record.staffList.map(s=>s.staffName).join(',')}}-->
          <!--</template>-->
          <template>
            <span
              v-for="(staff,index) in record.staffList"
              :key="index"
            >
              <span v-if="index !=0 ">,</span>
              <span
                v-if="staff.statusCode == 0 "
                style="color:rgba(0, 0, 0, 0.65)"
              >{{ staff.staffName }}</span>
              <span
                v-else-if="staff.statusCode == 2 "
                style="color:red;"
              >{{ staff.staffName }}</span>
              <span
                v-else-if="staff.statusCode != 0 && staff.statusCode != 2"
                style="color:red;"
                @click="handleApproveDetail(staff)"
              >{{ staff.staffName }}(审批信息)</span>
            </span>
          </template>
        </span>
        <span
          slot="employeeNum"
          slot-scope="text, record, index"
        >
          <template>
            <span
              v-for="(staff,index) in record.staffList"
              :key="index"
            >
              <span v-if="index !=0 ">,</span>
              <span
                v-if="staff.statusCode == 0 "
                style="color:rgba(0, 0, 0, 0.65)"
              >{{ staff.staffCode }}</span>
              <span
                v-else-if="staff.statusCode != 0 "
                style="color:red;"
              >{{ staff.staffCode }}</span>
            </span>
          </template>
        </span>
        <span
          slot="status"
          slot-scope="text, record, index"
        >
          <template>
            {{ record.staffList.map(s=>s.statusName).join(',') }}
          </template>
        </span>
        <span
          slot="operation"
          slot-scope="text, record, index"
        >
          <template>
            <a @click="handleSubOther(record)">查看</a>
            <span v-if=" type == 1 ">
              <a-divider type="vertical" /><a @click="handleEdit(record)">编辑</a>
            </span>
            <!--<span><a-divider type="vertical"/><a @click="handleApprove(record)">审批</a></span>-->
          </template>
        </span>
      </a-table>
      <title-name
        title="历史调整记录"
        style="margin-top: 20px;"
      />
      <list-table
        ref="table"
        :row-key="(record) => record.id"
        :columns="columnsRecord"
        :request="loadDataRecord"
      >
        <span
          slot="serial"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
      </list-table>

      <a-modal
        v-model="visibles"
        width="900px"
        :title="title"
      >
        <template slot="footer">
          <a-button
            key="back"
            @click="handleCancels"
          >
            关闭
          </a-button>
        </template>
        <a-form :model="form">
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-item
                label="岗位名称"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="form.jobName"
                  disabled
                />
              </a-form-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-item
                label="岗位类型"
                :label-col="{ lg: { span:5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="form.projectJobTypeName"
                  disabled
                />
              </a-form-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-item
                label="是否可兼任"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="form.canBothName"
                  disabled
                />
              </a-form-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-item
                label="是否可多人"
                :label-col="{ lg: { span:5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="form.numberLimitName"
                  disabled
                />
              </a-form-item>
            </a-col>
          </a-row>

          <a-row :gutter="24">
            <a-col
              :md="24"
              :sm="24"
            >
              <a-table
                :columns="columnsDetails"
                :data-source="dataSource"
                bordered
                :pagination="false"
              >
                <span
                  slot="serial"
                  slot-scope="text, record, index"
                >{{ index + 1 }}</span>
              </a-table>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col
              :md="24"
              :sm="24"
            >
              <div>
                <!--*备注：该人员之前的项目岗位权限，将在项目立项成功15天后，自动取消。-->
              </div>
            </a-col>
          </a-row>
        </a-form>
      </a-modal>

      <!--<add-adjust-->
      <!--ref="createModal"-->
      <!--:visible="visible"-->
      <!--:loading="confirmLoading"-->
      <!--v-on:employeeNums="employeeNums"-->
      <!--:model="mdl"-->
      <!--:projectId="projectId"-->
      <!--:isShowType="isShowType"-->
      <!--:groupDatas="groupDatas"-->
      <!--@cancel="handleCancel"-->
      <!--@ok="handleOk"-->
      <!--/>-->
      <a-modal
        v-if="visible"
        width="900px"
        :visible="true"
        :title="titles"
        @cancel="visible=false"
      >
        <template slot="footer">
          <a-button
            key="back"
            @click="handleCancel"
          >
            取消
          </a-button>
          <a-button
            key="submit"
            :loading="loading"
            type="primary"
            @click="handleOk"
          >
            确定
          </a-button>
        </template>
        <a-form-model
          ref="form1"
          :model="forms"
          :rules="rules"
        >
          <!--<a-form  :form="form" v-bind="formLayout" >-->
          <a-row :gutter="24">
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="调整类型"
                :label-col="{lg: {span: 7}, sm: {span: 7}}"
                :wrapper-col="{lg: {span:17}, sm: {span: 17} }"
                prop="type"
              >
                <a-select
                  v-model="forms.type"
                  placeholder="请选择调整类型"
                  show-search
                  @change="changeAdjust"
                >
                  <a-select-option
                    v-for="d in groupDatas"
                    :key="d.value"
                    :value="d.value"
                  >
                    {{ d.text }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>

            <a-col
              v-if="isReplace"
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="被更换人员"
                :label-col="{lg: {span: 7}, sm: {span: 7}}"
                :wrapper-col="{lg: {span:17}, sm: {span: 17} }"
                prop="origEmployeeId"
              >
                <a-select
                  v-model="forms.origEmployeeId"
                  placeholder="请选择被更换人员"
                >
                  <a-select-option
                    v-for="item in employees"
                    :key="item.staffId"
                    :value="item.staffId"
                  >
                    {{ item.staffName }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>

            <a-col
              v-if="isRemove"
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="被移除人员"
                :label-col="{lg: {span: 7}, sm: {span: 7}}"
                :wrapper-col="{lg: {span:17}, sm: {span: 17} }"
                prop="origEmployeeId"
              >
                <a-select
                  v-model="forms.origEmployeeId"
                  placeholder="请选择被移除人员"
                  @change="getStaffInfos"
                >
                  <a-select-option
                    v-for="item in employees"
                    :key="item.staffId"
                    :value="item.staffId"
                  >
                    {{ item.staffName }}
                  </a-select-option>
                  <!--<a-select-option v-for="item in employees" :key="item.staffId" :value="item.staffId" >{{item.staffName}}</a-select-option>-->
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>

          <div>
            <a-row :gutter="24">
              <a-col
                v-if="isShow"
                :md="12"
                :sm="12"
              >
                <a-form-model-item
                  label="调入人员名称"
                  :label-col="{lg: {span: 7}, sm: {span: 7}}"
                  :wrapper-col="{lg: {span:17}, sm: {span: 17} }"
                  prop="employee"
                >
                  <a-select
                    v-model="forms.employee"
                    show-search
                    placeholder="根据员工姓名搜索"
                    :not-found-content="fetching ?undefined : null"
                    :filter-option="filterOption"
                    label-in-value
                    :mode="selectMode"
                    @search="getProjectStaff"
                    @focus="getProjectStaff"
                    @change="getStaffInfo"
                    @popupScroll="scrollMore"
                  >
                    <a-icon
                      slot="suffixIcon"
                      type="search"
                    />
                    <a-select-option
                      v-for="item in staffs"
                      :key="item.id"
                      :value="item.id"
                    >
                      {{ item.name }}({{ item.jobNum }})
                    </a-select-option>
                    <!--<a-select-option v-for="item in staffs" :disabled="notPeples.filter((i) => i === item.id).length"  :key="item.id"  :value="item.id">{{item.name}}({{item.jobNum}})</a-select-option>-->
                  </a-select>
                </a-form-model-item>
              </a-col>
            </a-row>

            <a-row :gutter="24">
              <a-col
                :md="24"
                :sm="24"
              >
                <a-table
                  :columns="columnsFold"
                  :data-source="dataSources"
                  bordered
                  :pagination="false"
                />
              </a-col>
            </a-row>
            <a-row :gutter="24">
              <a-col
                :md="24"
                :sm="24"
              >
                <div>
                  *备注：该人员之前的项目岗位权限，将在人员调整成功15天后，自动取消。
                </div>
              </a-col>
            </a-row>
          </div>
        </a-form-model>
      </a-modal>
    </a-card>

    <ApprovalDetails
      :title="'审批详情'"
      :business-id="businessId"
      :visible="visibleApprove"
      @cancel="visibleApprove = false"
      @confirm="visibleApprove = false"
    />
    <!-- 选择审批人 -->
    <selectApprover
      :business-id="businessId"
      :process-config="setProcessConfig"
      :is-close-upper="false"
      @close="visible = false"
    />
    <!-- 选择审批人 -->
    <div class="page-btn-right-top">
      <a-button
        type="primary"
        @click="goBack"
      >
        关闭
      </a-button>
    </div>
  </base-breadcrumb>
</template>

<script>
import moment from 'moment'
import { getUserBranch, getUserProject, getDepartConfig, addDepartmentConfigs, getJobStaff, getJobEmployee, getNotConcurrentlyPeople } from '@/api/project/projects'
import { getAdjustList, getJobAdjust, getProjectJobAdjustsHistory } from '@/api/project/adjust'
import { getProjectStaff, getInfoByStaffId, getOrgUsers } from '@/api/commons'

import ARow from 'ant-design-vue/es/grid/Row'
import ApprovalDetails from '@/components/approvalDetails'

const columns = [
  {
    title: '序号',
    width: '65px',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '岗位名称',
    width: '200px',
    dataIndex: 'jobName',
    scopedSlots: { customRender: 'jobName' }
  },
  {
    title: '岗位人员姓名',
    width: '150px',
    dataIndex: 'employeeName',
    scopedSlots: { customRender: 'employeeName' }
  },
  {
    title: '员工编号',
    width: '150px',
    dataIndex: 'employeeNum',
    scopedSlots: { customRender: 'employeeNum' }
  },
  {
    title: '状态',
    width: '150px',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',

    dataIndex: 'operation',
    width: '150px',
    scopedSlots: { customRender: 'operation' }
  }
]

const columnsDetails = [
  {
    title: '序号',
    width: 65,
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '当时人员',
    dataIndex: 'origEmployeeName'
  },
  {
    title: '调整后人员',
    dataIndex: 'newEmployeeName'
  },
  {
    title: '调整时间',
    dataIndex: 'createTime'
  }
]

const columnsFold = [
  {
    title: '员工名称',
    dataIndex: 'employeeName'
  },
  {
    title: '员工编号',
    dataIndex: 'employeeNum'
  },
  {
    title: '所在单位',
    dataIndex: 'branch'
  },
  {
    title: '所在部门',
    dataIndex: 'projectName'
  },
  {
    title: '当前岗位',
    dataIndex: 'jobName'
  }
]
const columnsRecord = [
  {
    title: '序号',
    width: '65px',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '岗位名称',
    dataIndex: 'jobName'
  },
  {
    title: '调整类型',
    dataIndex: 'adjustType'
  },
  {
    title: '调整详情',
    dataIndex: 'adjustContent'
  },
  {
    title: '调整人',
    dataIndex: 'adjusterName'
  },
  {
    title: '调整时间',
    dataIndex: 'adjustTime'
  }
]

export default {
  name: 'a' + Date.now(),
  components: {
    ARow,
    ApprovalDetails
  },
  data() {
    this.columns = columns
    return {
      visibleApprove: false,
      pageNumber: 1, // 调入人员分页
      pageSize1: 20, // 调入人员分页数量
      totalPages: 0, // 总页数
      totalSize1: 0, // 总数
      unit: '',
      columnsFold,
      columnsDetails,
      columnsRecord,

      loading: false,

      titles: '项目人员调整',

      selectMode: 'default', //选择模式

      dataSources: [], // 导入人员基础数据

      isReplace: false, // 是否显示被更换人员
      isRemove: false, // 是否显示被移除人员
      isShow: false, // 新增

      rules: {
        type: [{ required: true, message: '请选择调整类型', trigger: 'change' }],
        origEmployeeId: [{ required: true, message: '请选择移除/更换人员', trigger: 'change' }],
        employee: [{ required: true, message: '请选择调入人员', trigger: 'change' }]
      },

      forms: {},
      employees: [],
      employeeIds: [],
      staffs: [], // 调入人员名称
      notPeples: [], // 不可调入人员名称

      fetching: false,

      employeeStatus: '0',
      title: '岗位信息',
      dataSource: [],
      form: {}, // 详情对象

      currentNum: '', // 当前人员数量
      managerNum: '', // 领导班子人员数量
      otherNum: '', // 其他岗位人员数量

      titleName: '',
      // create model
      visible: false,
      visibles: false,

      confirmLoading: false,
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        companyName: '', // 公司名称
        projectName: '' // 项目名称
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getAdjustList(requestParameters).then((res) => {
          return res
        })
      },
      loadDataRecord: (parameter) => {
        const requestParameters = Object.assign({query:{projectId: this.projectId}}, parameter)
        return getProjectJobAdjustsHistory(requestParameters).then((res) => {
          return res
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      companyNames: [], // 分公司名称

      projectPost: [], // 项目人员岗位设置
      otherPost: [], //  其他人员岗位设置
      employeeNum: '', //  人员编号

      projectId: '', // 项目id
      branchName: '', // 项目id
      projectName: '', // 项目id

      groupDatas: [], // 调整类型
      isShowType: 0, // 显示调整类型
      type: '',
      delStaffList: [], // 不能选中当前人员
      // 流程配置
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      // 业务id
      businessId: null
    }
  },

  async created() {
    this.projectId = this.$route.query.projectId
    this.type = this.$route.query.type
    this.branchName = this.$route.query.branchName
    this.projectName = this.$route.query.projectName
    this.unit = this.branchName + '/' + this.projectName
    this.getPosts(this.projectId)
    //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('project_department_personnel_adjustment')
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        selectedRows: this.selectedRows, // 当前行所有数据
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    handleApproveDetail(record) {
      this.visibleApprove = true
      this.businessId = record.adjustId
    },
    // 下拉滚动加载
    scrollMore(e) {
      const { target } = e
      var total = target.scrollTop + target.offsetHeight
      var scrollHeight = target.scrollHeight

      // this.pageNo是当前页 this.totalPage是总页数
      if (total >= scrollHeight - 3 && total < scrollHeight + 3 && this.pageNumber <= this.totalPages) {
        this.pageNumber = this.pageNumber + 1
        this.getProjectStaff() // 调用api方法
      }
    },

    getNotConcurrentlyPeople() {
      getNotConcurrentlyPeople({ projectId: this.projectId }).then((res) => {
        this.notPeples = res.data
      })
    },
    filterByName(aim, statusCode) {
      // 移除和更换是只能选择在岗人员
      return aim.filter((item) => item.statusCode == statusCode)
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    getProjectStaff(value) {
      this.fetching = true
      // 不需要查已删除人员(delete:0)；只查公司内部人员(type:0)
      getOrgUsers({ key: value ? value : '', pageNumber: this.pageNumber, pageSize: this.pageSize1, delete: 0, type: 0 })
        .then((res) => {
          if (res.data) {
            // 去重, 如果已存在，则不再添加
            let data = []
            res.data &&
              res.data.forEach((item) => {
                if (!this.staffs.filter((itemc) => item.id == itemc.id).length) {
                  data.push(item)
                }
              })
            this.staffs = this.staffs.concat(data)

            this.staffs = this.staffs.filter((item) => !this.delStaffList.some((ele) => ele.employeeId === item.id))
          }
          this.totalPages = res.totalPages
          this.totalSize1 = res.totalSize
          this.fetching = false
        })
        .catch(() => {
          this.fetching = false
        })
    },
    includes(arr1, arr2) {
      return arr2.every((val) => arr1.includes(val))
    },

    getStaffInfo(value) {
      // 获取人员分公司  岗位信息
      let arr = []
      arr.push(value.key)
      this.dataSources = []
      if (arr.length > 0) {
        getJobEmployee({ ids: arr, projectAuditStatus: 2, employeeStatus: 0 }).then((res) => {
          this.dataSources = res.data
        })
      }
    },
    getStaffInfos(value) {
      // 获取人员分公司  岗位信息
      const employee = this.employees.find((item) => item.staffId == value)

      console.log('当前对象666：', employee)
      this.dataSources = []
      getJobEmployee({ ids: [employee.employeeId], projectAuditStatus: 2, employeeStatus: 0 }).then((res) => {
        // getJobEmployee({ids:[value],projectAuditStatus:2}).then(res=>{
        this.dataSources = res.data
      })
    },
    changeAdjust(item) {
      if (item == 2) {
        // 新增
        this.isShow = true
        this.isReplace = false
        this.isRemove = false
      }
      if (item == 0) {
        // 更换
        this.isShow = true
        this.isReplace = true
        this.isRemove = false
      }
      if (item == 1) {
        // 移除
        this.isRemove = true
        this.isReplace = false
        this.isShow = false
      }
    },

    goBack() {
      this.$multiTab.close(this.$route.fullpath)
    },
    employeeNums(val) {
      this.employeeNum = val
    },
    // getUserBranch() {    // 根据用户查询分公司
    //   getUserBranch().then(res => {
    //     this.companyNames = res.data
    //     this.queryParam.companyName = (res.data)[0].code
    //     let companyCode = (res.data)[0].code
    //     getUserProject(companyCode).then(reponse => {
    //       // this.queryParam.projectName = (reponse.data)[0].name
    //       let projectId = (reponse.data)[0].code
    //       this.projectId = (reponse.data)[0].code
    //
    //       getJobStaff(projectId).then(resData=>{
    //           this.currentNum = resData.data.total  // 当前人员数量
    //           this.managerNum = resData.data.leaders  // 领导班子人员数量
    //           this.otherNum = resData.data.others    // 其他岗位人员数量
    //       })
    //
    //       getDepartConfig({ projectId: projectId, projectJobType: 1 }).then(resData1 => {
    //         this.projectPost = resData1.data.postConfigList
    //       })
    //
    //       getDepartConfig({ projectId: projectId, projectJobType: 0 }).then(resData2 => {
    //         this.otherPost = resData2.data.postConfigList
    //         console.log("hkjfshfakjg1:",resData2.data.postConfigList)
    //       })
    //     })
    //   })
    // },

    getPosts(value) {
      this.projectId = value
      getJobStaff(value).then((res) => {
        this.currentNum = res.data.total // 当前人员数量
        this.managerNum = res.data.leaders // 领导班子人员数量
        this.otherNum = res.data.others // 其他岗位人员数量
      })
      getDepartConfig({ projectId: value, projectJobType: 1 }).then((resData1) => {
        this.projectPost = resData1.data.postConfigList
      })

      getDepartConfig({ projectId: value, projectJobType: 0 }).then((resData2) => {
        this.otherPost = resData2.data.postConfigList
      })
    },

    handleEdit(record) {
      console.log('当前行数据：', record)
      this.visible = true
      this.forms = {}
      this.mdl = record
      this.dataSources = []

      this.isShow = false
      this.isReplace = false
      this.isRemove = false

      this.employees = this.filterByName(record.staffList, 0)

      this.delStaffList = record.staffList

      this.employeeIds = this.employees.map((s) => s.employeeId)

      this.groupDatas = []
      if (record.staffList.length == 0 || record.numberLimitCode == 1) {
        this.groupDatas.push({ value: 2, text: '新增' })
      }

      if (record.staffList.length != 0) {
        this.groupDatas.push({ value: 0, text: '更换' })
      }

      if ((record.staffList.length == 0 || record.staffList.length == 1) && record.requiredCode == 1 && record.staffList[0].statusCode == 0) {
      } else {
        this.groupDatas.push({ value: 1, text: '移除' })
      }

      this.groupDatas = this.deWeight(this.groupDatas)
    },
    deWeight(arr) {
      for (var i = 0; i < arr.length - 1; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i].value == arr[j].value) {
            arr.splice(j, 1)
            //因为数组长度减小1，所以直接 j++ 会漏掉一个元素，所以要 j--
            j--
          }
        }
      }
      return arr
    },

    // handleApprove(val){
    //   this.$router.push({path:'/project/staffAdjust/approve',query:{id:val.id}})
    // },
    handleOk() {
      let that = this

      that.$refs.form1.validate((errors, values) => {
        if (errors) {
          if (that.forms.origEmployeeId != '' && that.forms.origEmployeeId != undefined) {
            const target = that.employees.find((item) => item.staffId == that.forms.origEmployeeId)
            that.forms.origEmployeeId = target.staffId
          }
          that.forms.projectId = that.projectId
          that.forms.projectJobId = that.mdl.jobId

          if (that.forms.employee != undefined) {
            let pythonpath = that.forms.employee.label
            let index = pythonpath.lastIndexOf('(')
            let Writepathexe = pythonpath.substring(0, index)

            let re = /\([^\)]+\)/g
            let s = that.forms.employee.label
            if (s.match(re) != null) {
              s = s.match(re)[0]

              s = s.substring(1, s.length - 1)
            } else {
              s = null
            }

            that.forms.employee.label = Writepathexe
            that.forms.employeeNum = s
          }

          that.loading = true
          this.setProcessConfig = null
          this.submitApprovalConfirm(this.processConfig,(flag)=>{
            if(!flag) {
              that.loading = false
              return
            }
            addDepartmentConfigs(that.forms)
                    .then((res) => {
                      that.loading = false
                      console.log(res)
                      if (res.code == 200) {
                        that.$message.success('调整成功')
                        if(that.forms.type*1===1){   //移除
                           that.$refs.table.refresh(true)
                        }
                
                        getDepartConfig({ projectId: that.projectId, projectJobType: 1 }).then((resData1) => {
                          that.projectPost = resData1.data.postConfigList
                        })

                        getDepartConfig({ projectId: that.projectId, projectJobType: 0 }).then((resData2) => {
                          that.otherPost = resData2.data.postConfigList
                        })
                        this.businessId = res.data
                        this.setProcessConfig = this.processConfig
                        if(this.processConfig*1===4){
                            that.visible = false
                        }
                      } else {
                        that.$message.error('新增失败')
                      }
                    })
                    .catch(() => {
                      that.loading = false
                    })
          })

        }else {
          this.$message.warning("请填写完所有必填项信息")
        }
      })
    },
    handleCancel() {
      this.visible = false
      // const form = this.$refs.createModal.form
      // form.resetFields() // 清理表单数据（可不做）
    },
    handleSub(record) {
      this.visibles = true
      this.form = record
      this.form.projectJobTypeName = '领导班子'

      getJobAdjust({ jobId: record.jobId, pageNo: 1, pageSize: 1000, auditStatus: 1 }).then((res) => {
        this.dataSource = res.data
      })
    },
    handleSubOther(record) {
      this.visibles = true
      this.form = record
      this.form.projectJobTypeName = '其他岗位'

      getJobAdjust({ jobId: record.jobId, pageNo: 1, pageSize: 1000, auditStatus: 1 }).then((res) => {
        this.dataSource = res.data
      })
    },
    handleCancels() {
      this.visibles = false
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
<style>
.requireRed {
  color: red;
}
</style>
